﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Telerik.Web.UI;
using Medianamik.Globalization;
using Medianamik.UI.Controls;

namespace Medianamik.UI.ContentManagement
{
    public partial class TemplateList : BaseModuleListPage
    {
        protected void Page_Init(object sender, EventArgs e)
        {

            var webSites = ConfigManager.GetAll<WebSite>();

            int webSiteCount = webSites.Count();

            if (webSiteCount > 1)
            {
                BaseModalWindow webSiteSelectorWindow = new BaseModalWindow();
                webSiteSelectorWindow.ID = "WebSiteSelectorWindow";

                if (!IsPostBack)
                {
                    webSiteSelectorWindow.OnClientClose = "onWebSiteSelected";
                    //TODO: Mettre bon icone
                    webSiteSelectorWindow.IconUrl = "/Medianamik/images/Icones/Ico_AjoutPage.gif";
                    webSiteSelectorWindow.ReloadOnShow = true;
                    webSiteSelectorWindow.Width = Unit.Pixel(400);
                    webSiteSelectorWindow.Height = Unit.Pixel(200);

                    AddTemplateLink.Attributes.Add("onclick",
                        "GetRadWindowManager().Open(null, 'WebSiteSelectorWindow');");
                    AddTemplateLink.Target = "WebSiteSelectorWindow";
                }

                MyWindowManager.Windows.Add(webSiteSelectorWindow);
            }
            else
            {
                if (!IsPostBack)
                {
                    if (webSiteCount > 0)
                    {
                        _managementURL += "?" + QueryStringParamNames.WebSiteId + "="+ webSites.First().ID.ToString();
                        AddTemplateLink.NavigateUrl = ManagementURL;
                    }
                    else{
                        AddTemplateLink.Enabled = false;
                        //TODO: Afficher message expliquant pourquoi on ne peut pas ajouter de gabarits
                    }
                }
            }
        }

        protected override void GridNeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            templateGrid.DataSource = ConfigManager.GetAll<MedianamikTemplate>(CurrentUICulture)
                 .OrderBy(m => m.WebSiteName)
                 .ThenBy(m => m.Name);
        }

        protected override RadGrid Grid
        {
            get { return templateGrid; }
        }

        protected override UpdatePanel GridUpdatePanel
        {
            get { return mainUpdatePanel; }
        }

        protected override string ReturnUrl
        {
            get { return "/medianamik/contentmanagement/templatelist.aspx"; }
        }

        protected override string ManagementURL
        {
            get { return _managementURL; }
        }
        string _managementURL = "/medianamik/contentmanagement/templatemanagement.aspx";
    }
}
